package com.zxl.live.screen.call.model;

import android.content.Context;
import android.text.TextUtils;

import com.zxl.live.screen.R;
import com.zxl.live.screen.call.model.bean.ThemeBean;
import com.zxl.live.screen.screen.constant.LockerEnv;
import com.zxl.live.tools.common.ApplicationHelper;
import com.zxl.live.tools.common.Observable;
import com.zxl.live.tools.tools.FileUtil;
import com.zxl.live.tools.tools.HttpUtils;
import com.zxl.live.tools.tools.TimeUtils;

import org.json.JSONArray;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/5.
 */

public class ThemeModel extends Observable<ThemeBean, Integer, List<ThemeBean>> {

    private String filePath;

    private String[] mIconArrays;
    private String[] mNumberArrays;
    private String[] mNameArrays;
    private String[] mColorArrays;

    public ThemeModel() {
        filePath = LockerEnv.Path.CALL + "call_flash";
    }

    @Override
    public void run() {
        Context context = ApplicationHelper.getContext();
        mIconArrays = context.getResources().getStringArray(R.array.call_default_icon);
        mNameArrays = context.getResources().getStringArray(R.array.call_default_name);
        mNumberArrays = context.getResources().getStringArray(R.array.call_default_number);
        mColorArrays = context.getResources().getStringArray(R.array.call_default_color);

        File file = new File(filePath);
        if (file.exists() && System.currentTimeMillis() - file.lastModified() < 2 * TimeUtils.DAY) {
            String content = FileUtil.readFileToString(file.getPath());
            if (!TextUtils.isEmpty(content)) {
                parseData(content);
            }
            return;
        }
        requestFromNet();
    }

    private void requestFromNet() {
        String url = "http://zxl-public.oss-cn-shenzhen.aliyuncs.com/callLock/call_flash";
        byte[] bs = HttpUtils.doGet(url);
        if (null != bs && bs.length > 0) {
            parseData(new String(bs));
            FileUtil.saveByteToSDFile(bs, filePath);
        } else {
            handleFail(ThemeBean.ERROR_REQUEST_NET);
        }
    }

    private void parseData(String content) {
        try {
            List<ThemeBean> list = new ArrayList<>();
            JSONArray jsonArray = new JSONArray(content);
            if (null != jsonArray && jsonArray.length() > 0) {
                for (int i = 0; i < jsonArray.length(); i++) {
                    ThemeBean ringtoneBean = new ThemeBean();
                    ringtoneBean.defaultColor = mColorArrays[i % mColorArrays.length];
                    ringtoneBean.defaultName = mNameArrays[i % mNameArrays.length];
                    ringtoneBean.defaultNumber = mNumberArrays[i % mNumberArrays.length];
                    ringtoneBean.defaultIcon = mIconArrays[i % mIconArrays.length];
                    ringtoneBean.parse(jsonArray.optJSONObject(i));
                    list.add(ringtoneBean);
                }
            }
            handleComplete(list);
        } catch (Exception e) {
            e.printStackTrace();
            handleFail(ThemeBean.ERROR_EXCEPTION);
        }
    }
}
